// pages/activitys/maverickBuy/maverickBuy.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess:{
      navtitle: "购物车",
      backhide: 0,
      topheight: 1
    },
    count: 0,   //合计
    number: 0,  //件数
    allSelect: false,
    demolist: [
      {
        "title": "【项目】日本贴片美甲",
        "imageUrl": "../../images/picture3.png",
        "price": 100,
        "vipprice": 200,
        "oldprice": 50,
        "num": "0", 
        "selected": false
      },
      {
        "title": "【项目】日本贴片美甲",
        "imageUrl": "../../images/picture3.png",
        "price": "100.00",
        "vipprice": "200.00",
        "oldprice": "50.00",
        "num": "0", 
        "selected": false
      },
      {
        "title": "【项目】日本贴片美甲",
        "imageUrl": "../../images/picture3.png",
        "price": "100.00",
        "vipprice": "200.00",
        "oldprice": "50.00",
        "num": "0", 
        "selected": false
      },
      {
        "title": "【项目】日本贴片美甲",
        "imageUrl": "../../images/picture3.png",
        "price": "100.00",
        "vipprice": "200.00",
        "oldprice": "50.00",
        "num": "0", 
        "selected": false
      }
    ]
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  bindMinus: function (e) {
    var index = parseInt(e.currentTarget.dataset.index),
        num = this.data.demolist[index].num,
        price = this.data.demolist[index].price;
    var demolist = this.data.demolist;
    if(demolist[index].selected && num>0){
      num--;
      this.setData({
        count: this.data.count - 1 * price,
        number: this.data.number - 1
      });
    }else if(num>0){
      num--;
    }
    demolist[index].num = num;
    this.setData({
      demolist: demolist
    });
  },
  bindPlus: function (e) {
    var index = parseInt(e.currentTarget.dataset.index),
        num = this.data.demolist[index].num,
        price = this.data.demolist[index].price;
    var demolist = this.data.demolist;
    if(demolist[index].selected){
      num++;
      this.setData({
        count: this.data.count + 1 * price,
        number: this.data.number + 1
      });
    }else{
      num++;
    }
    demolist[index].num = num;
    this.setData({
      demolist: demolist
    });
  },
  bindCheckbox: function (e) {
    var index = parseInt(e.currentTarget.dataset.index), 
        selected = this.data.demolist[index].selected,
        num = parseInt(this.data.demolist[index].num),
        price = this.data.demolist[index].price;
    var demolist = this.data.demolist;
        demolist[index].selected = !selected;
    this.setData({
      demolist: demolist
    });
    if (!selected) {
      this.setData({
        count: this.data.count + num * price,
        number: this.data.number + num 
      });
    } else {
      this.setData({
        count: this.data.count - num * price,
        number: this.data.number - num
      });
    }
    // 自动全选
    console.log("全选",this.checkisall())
    if(this.checkisall()){
      this.setData({
        allSelect: true
      })
    }else{
      this.setData({
        allSelect: false
      })
    }
  },
  bindSelectAll: function () {
    var that = this;
    that.setData({
      allSelect: !that.data.allSelect
    })
    var demolist = that.data.demolist,
        count,number;

    if (that.data.allSelect) {
      count = 0;number = 0;
      for (var i = 0; i < demolist.length; i++) {
        var num = parseInt(that.data.demolist[i].num),
            price = that.data.demolist[i].price;
          if(!demolist[i].selected){
              demolist[i].selected = true;
          }
          number += num;
          count += num * price;
      }
    }else{
      count = 0;number = 0;
      for (var i = 0; i < demolist.length; i++) {
          if(demolist[i].selected){
              demolist[i].selected = false;
          }
      }
    }
    that.setData({
      demolist: demolist,
      count: count,
      number: number
    })
  },
  // 检测全选功能
  checkisall: function(){
    var demolist = this.data.demolist,
        res = true;
    for (var i = 0; i < demolist.length; i++) {
        if(!demolist[i].selected){
          res = false;
        }
    }
    return res
  },

  //手指触摸动作开始 记录起点X坐标
  touchstart: function(e) {
    //开始触摸时 重置所有删除
    let data = app.touch._touchstart(e, this.data.demolist)
    this.setData({
      demolist: data
    })
  },
 
  //滑动事件处理
  touchmove: function(e) {
    let data = app.touch._touchmove(e, this.data.demolist)
    this.setData({
      demolist: data
    })
  },
 
  //删除事件
  del: function(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认要删除此条信息么？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.data.demolist.splice(e.currentTarget.dataset.index, 1)
          that.setData({
            demolist: that.data.demolist
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 结算传参
  settle: function(e){
    var settlearr = [],
        demolist = this.data.demolist;
    for(var i = 0;i< demolist.length;i++){
      if(demolist[i].selected){
        settlearr.push(demolist[i]);
      }
    }
    if(!settlearr.length){
      wx.showToast({
        title: '请选择结算商品',
        icon: 'none'
      })
    }else{
      wx.navigateTo({
        url: e.currentTarget.dataset.url + "?settlearr=" + JSON.stringify(settlearr),
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareappMessage: function () {

  }
})